import { useEffect, useState } from 'react'
import logo from './logo.svg'
import './App.css'

function App() {
  const [count, setCount] = useState(0)

  function setMessage(data) {
    console.log('父组件的 count:',count);
    
    console.log('收到子组件通过回调函数传来的state:',data);
    setCount(data);
    
    console.log('父组件的 count:',count);


  }

  return (
    <div className="App">
      count:{count}
      <Button setMessageToParent={setMessage} />
    </div>
  )
}

export default App

function Button(props) {
  const [state,setState] = useState(0);

  const click = () => {
      
    console.log('点击了一次子组件，state:',state);
    
    setState(state + 1);
    

    console.log('子组件增加了1之后,state:',state);
    props.setMessageToParent(state);
    console.log('点击事件完毕---');

  }

  const click1 = () => {
    console.log(state);
    setState(state+1);
    console.log(state);
  }

  console.log.call(this,'1:',state);
  
 

  useEffect(() => {
    console.log.call(this,'in useEffect:',state);
      
  },[state])

  setTimeout(() => {
    console.log.call(this,'in setimeout:',state);
    setState(2);
    console.log.call(this,'in setimeout:',state);

  },2000)

  return (
    <>
      state:{state}
      <button onClick={click1}>点击</button>
      
    </>

  )
}